package com.mc.api.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.xdevapi.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class TestRest {

//    @Autowired
//    private static RestTemplate restTemplate;
public static String doGet(String httpurl) {
    HttpURLConnection connection = null;
    InputStream is = null;
    BufferedReader br = null;
    String result = null;// 返回结果字符串
    try {
        // 创建远程url连接对象
        URL url = new URL(httpurl);
        // 通过远程url连接对象打开一个连接，强转成httpURLConnection类
        connection = (HttpURLConnection) url.openConnection();
        // 设置连接方式：get
        connection.setRequestMethod("GET");
        // 设置连接主机服务器的超时时间：15000毫秒
        connection.setConnectTimeout(15000);
        // 设置读取远程返回的数据时间：60000毫秒
        connection.setReadTimeout(60000);
        // 发送请求
        connection.connect();
        // 通过connection连接，获取输入流
        if (connection.getResponseCode() == 200) {
            is = connection.getInputStream();
            // 封装输入流is，并指定字符集
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            // 存放数据
            StringBuffer sbf = new StringBuffer();
            String temp = null;
            while ((temp = br.readLine()) != null) {
                sbf.append(temp);
                sbf.append("\r\n");
            }
            result = sbf.toString();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 关闭资源
        if (null != br) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        connection.disconnect();// 关闭远程连接
    }

    return result;
}
    public static String selectObjectId(String code){
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
        params.add("where","code = \"15\"");
        params.add("geometryType","esriGeometryEnvelope");
        params.add("spatialRel","esriSpatialRelIntersects");
        params.add("units","esriSRUnit_Foot");
        params.add("outFields","OBJECTID");
        params.add("returnGeometry","false");

        params.add("returnDistinctValues","false");
        params.add("returnIdsOnly","false");
        params.add("returnCountOnly","false");
        params.add("returnExtentOnly","false");
        params.add("returnZ","false");
        params.add("returnM","false");
        params.add("multipatchOption","xyFootprint");
        params.add("returnTrueCurves","false");
        params.add("returnExceededLimitFeatures","false");
        params.add("returnCentroid","false");
        params.add("sqlFormat","none");
        params.add("featureEncoding","esriDefault");
        params.add("f","pjson");



        String body = null;
        try {
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, null);

            ResponseEntity<String> forEntity = restTemplate.postForEntity("http://gisserver2.mengcao.com/server/rest/services/germplasm/GpmBlankService/FeatureServer/0/query", requestEntity, String.class);
            body = forEntity.getBody();
        } catch (Exception e) {
        }
        return JSONObject.parseObject(body).getJSONArray("features").getJSONObject(0).getJSONObject("attributes").getString("OBJECTID");
    }

    public static void main(String[] args) {

        System.out.println(selectObjectId("15"));
        //Map<Object,Object> body = response.getBody();
    }

}
